/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs.workManagerServer.classLoaders;

import java.io.IOException;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import org.apache.log4j.Logger;
import pl.put.dcs.workManagerServer.classLoaders.exceptions.LibraryClassLoaderException;
import pl.put.dcs.workManagerServer.classLoaders.jclClassLoaders.ExtendJarLoader;
import xeus.jcl.JarClassLoader;
import xeus.jcl.JclObjectFactory;
import xeus.jcl.JclUtils;
import xeus.jcl.exception.JclException;

/**
 *
 * @author LukaszR
 */
public class LibraryClassLoader
{

    public static Logger log = Logger.getLogger( LibraryClassLoader.class.getName() );
    private static LibraryClassLoader instance = null;
    private ExtendJarLoader jcl;
    private JclObjectFactory factory;

    private LibraryClassLoader()
    {
        jcl = new ExtendJarLoader();
        //JarClassLoader();
        factory = JclObjectFactory.getInstance();
    }

    public static void addJar( String path ) throws LibraryClassLoaderException
    {
        LibraryClassLoader jcl = getInstance();
        jcl.addJarToClassPath( path );
    }

    public void addJarToClassPath( String path ) throws LibraryClassLoaderException
    {
        try
        {
            jcl.add( path );
        }
        catch ( IOException ex )
        {
            log.error( "ioEX" + ex.getMessage() );
            throw new LibraryClassLoaderException( ex );
        }
        catch ( JclException ex )
        {
            log.error( "JCLEX" + ex.getMessage() );
            throw new LibraryClassLoaderException( ex );
        }
    }

    public static LibraryClassLoader getInstance()
    {
        if ( instance == null )
        {
            instance = new LibraryClassLoader();
        }
        return instance;
    }

    public Object create( String className, Object[] args ) throws LibraryClassLoaderException
    {
        try
        {
            return factory.create( jcl, className, args );
        }
        catch ( IOException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( ClassNotFoundException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( InstantiationException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( IllegalAccessException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( IllegalArgumentException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( SecurityException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( InvocationTargetException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( NoSuchMethodException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }

    }

    public Object create( String className ) throws LibraryClassLoaderException
    {
        try
        {
            return factory.create( jcl, className );
        }
        catch ( IllegalArgumentException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( SecurityException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( IOException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( ClassNotFoundException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( InstantiationException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( IllegalAccessException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( InvocationTargetException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
        catch ( NoSuchMethodException ex )
        {
            throw new LibraryClassLoaderException( ex );
        }
    }

    public InputStream getResourceAsStream( String name )
    {

        return jcl.getResourceAsStream( name );
    }

    public static java.io.InputStream getResourceAsStreamFromClassPath( String name )
    {
        LibraryClassLoader loaderInstance = LibraryClassLoader.getInstance();
        return loaderInstance.getResourceAsStream( name );
    }
}
